<?php

namespace app\admin\controller;

use app\BaseController;
use Swoole\Coroutine;
use function Swoole\Coroutine\run;

class Client extends BaseController
{
    public function index()
    {
        run(function () {
            $socket = new Coroutine\Socket(AF_INET, SOCK_STREAM, 0);

            $retval = $socket->connect('127.0.0.1', 9501);
            while ($retval)
            {
                $n = $socket->send('hello');
                var_dump($n);

                $data = $socket->recv();
                var_dump($data);

                //发生错误或对端关闭连接，本端也需要关闭
                if ($data === '' || $data === false) {
                    echo "errCode: {$socket->errCode}\n";
                    $socket->close();
                    break;
                }

                Coroutine::sleep(1.0);
            }

            var_dump($retval, $socket->errCode, $socket->errMsg);
        });
    }
}